﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;
using System.Text;
using CarRentalWeb.AutorentWebServiceReference;

namespace Autorent.Web.handlers
{
    /// <summary>
    /// Summary description for PlaceOrderHandler
    /// </summary>
    public class PlaceOrderHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                HttpRequest request = context.Request;
                int carId;
                Int32.TryParse(request.Params["carId"].ToString(),out carId);
                string category = request.Params["category"].ToString();
                int location;
                Int32.TryParse(request.Params["location"].ToString(), out location);
                DateTime dateStart = DateTime.Parse(request.Params["dateStart"]);
                DateTime dateEnd = DateTime.Parse(request.Params["dateEnd"]);
                string timeStart = request.Params["timeStart"].ToString();
                string timeEnd = request.Params["timeEnd"].ToString();
                string tariff = request.Params["tariff"].ToString();
                string email = request.Params["email"].ToString();


                //sending information to service and receiving ticket number
                AutoRentEntities entity = new AutoRentEntities(new Uri(System.Web.Configuration.WebConfigurationManager.AppSettings["service"]));
                AutoOrder order = new AutoOrder()
                {
                    Auto_ID = carId,
                    AutoClass_ID = entity.AutoClasses.FirstOrDefault(a => a.AutoClassName == category).AutoClass_ID,
                    Off_Office_ID = entity.Offices.FirstOrDefault(a => a.Office_ID == location).Office_ID,
                    AutoOrderRequestDT = dateStart,
                    AutoOrderReturningDT = dateEnd,
                    AutoOrderClientEmail = email
                };
                //create email text
                StringBuilder sb = new StringBuilder();
                sb.Append("Thank you for choosing our service, you request ID: ");
                sb.Append("31" + System.Environment.NewLine);
                sb.Append("You can check your order details on our web-site" + System.Environment.NewLine);
                sb.Append(request.Url);
                sb.Append("#checkout");

                //send email to customer
                this.SendEmail(email, sb.ToString());
                context.Response.Write("31");
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message.ToString());
                System.Diagnostics.Trace.WriteLine(ex.Message + " " + DateTime.Now.ToString());
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private void SendEmail(string adress, string text)
        {
            MailAddress mailfrom = new MailAddress(System.Web.Configuration.WebConfigurationManager.AppSettings["email"]);
            MailAddress mailto = new MailAddress(adress);
            MailMessage newmsg = new MailMessage(mailfrom, mailto);

            newmsg.Subject = "Order confirmation notification";
            newmsg.Body = text;

            SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential(System.Web.Configuration.WebConfigurationManager.AppSettings["login"],
                                                     System.Web.Configuration.WebConfigurationManager.AppSettings["credential"]);
            smtp.EnableSsl = true;
            smtp.Send(newmsg);
        }
    }
}